package cn.sym.system.listener;

import cn.sym.basic.util.JwtRasHolder;
import cn.sym.system.service.IPermissionScanService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
//申明自定义的web监听器，被容器注册和使用
@WebListener
public class PermissionScanListener implements ServletContextListener {
    @Autowired
    private IPermissionScanService iPermissionScanService;
    @Value("${jwt.rsa.pub}")
    private String jwtRsaPub;
    @Value("${jwt.rsa.pri}")
    private String jwtRsaPrivate;

    @SneakyThrows
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //匿名内部类写法
        new Thread(new Runnable() {
            @Override
            public void run() {
                iPermissionScanService.annoScan();
            }
        }).start();
        JwtRasHolder.INSTANCE.setJwtRsaPubData(FileCopyUtils
                .copyToByteArray(this.getClass().getClassLoader().getResourceAsStream(jwtRsaPub)));
        JwtRasHolder.INSTANCE.setJwtRsaPrivateData(FileCopyUtils
                .copyToByteArray(this.getClass().getClassLoader().getResourceAsStream(jwtRsaPrivate)));
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
